package com.proj.hot.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.activity.LoginActivity;
import com.proj.hot.activity.VODBuyPlayActivity;
import com.proj.hot.utils.DebugUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 旅游
 * Created by zangchenggang on 17/6/30.
 */
public class TravelFragment extends BaseFragment {

    private List<View> viewList=new ArrayList<>();
    private View tra_view1,tra_view2,tra_view3,tra_view4,tra_view5,tra_view6,tra_view7,tra_view8;
    private ImageView poster1,poster2,poster3,poster4,poster5,poster6,poster7,poster8;
    JSONArray dataArray;
    String code;

    private TextView text1, text2, text3, text4, text5, text6, text7, text8;


    private Handler handler =  new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                setView();

            }
        }
    };


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (code!=null){
            getData();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_travel,container,false);

        tra_view1 = rootView.findViewById(R.id.tra_rl_poster1);
        tra_view2 = rootView.findViewById(R.id.tra_rl_poster2);
        tra_view3 = rootView.findViewById(R.id.tra_rl_poster3);
        tra_view4 = rootView.findViewById(R.id.tra_rl_poster4);
        tra_view5 = rootView.findViewById(R.id.tra_rl_poster5);
        tra_view6 = rootView.findViewById(R.id.tra_rl_poster6);
        tra_view7 = rootView.findViewById(R.id.tra_rl_poster7);
        tra_view8 = rootView.findViewById(R.id.tra_rl_poster8);
        viewList.add(tra_view1);
        viewList.add(tra_view2);
        viewList.add(tra_view3);
        viewList.add(tra_view4);
        viewList.add(tra_view5);
        viewList.add(tra_view6);
        viewList.add(tra_view7);
        viewList.add(tra_view8);


        poster1=(ImageView)rootView.findViewById(R.id.tra_poster1);
        poster2=(ImageView)rootView.findViewById(R.id.tra_poster2);
        poster3=(ImageView)rootView.findViewById(R.id.tra_poster3);
        poster4=(ImageView)rootView.findViewById(R.id.tra_poster4);
        poster5=(ImageView)rootView.findViewById(R.id.tra_poster5);
        poster6=(ImageView)rootView.findViewById(R.id.tra_poster6);
        poster7=(ImageView)rootView.findViewById(R.id.tra_poster7);
        poster8=(ImageView)rootView.findViewById(R.id.tra_poster8);

        text1=(TextView) rootView.findViewById(R.id.tra_text1);
        text2=(TextView) rootView.findViewById(R.id.tra_text2);
        text3=(TextView) rootView.findViewById(R.id.tra_text3);
        text4=(TextView) rootView.findViewById(R.id.tra_text4);
        text5=(TextView) rootView.findViewById(R.id.tra_text5);
        text6=(TextView) rootView.findViewById(R.id.tra_text6);
        text7=(TextView) rootView.findViewById(R.id.tra_text7);
        text8=(TextView) rootView.findViewById(R.id.tra_text8);

        setOnListener();
        return rootView;
    }

    private void setOnListener(){

        for (int i =0;i<viewList.size();i++){
            final int k=i;
            viewList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (GlobalParams.IS_LOGIN){
                    Intent intent = new Intent(getActivity().getApplicationContext(), VODBuyPlayActivity.class);
                    try {
                        intent.putExtra("contentid",dataArray.getJSONObject(k).getString("recode"));
                        intent.putExtra("goodsORtrave","trave");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    startActivity(intent);
                    }else{
                        Intent intent = new Intent(getActivity().getApplicationContext(), LoginActivity.class);
                        startActivity(intent);
                    }
                }
            });
        }
    }



    public void setCodeAndInitData(String code) {

        this.code=code;

    }


    private void getData(){

        try {



            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("code", code);

            body.put("sn", GlobalParams.SN);

            RequestParams params = new RequestParams(GlobalParams.reqHost + "featuredFirst/getDetails?"+code);
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        JSONObject jData=new JSONObject(s);

                        JSONObject respbody=jData.getJSONObject("respbody");
                        dataArray=respbody.getJSONArray("data");
                        handler.sendEmptyMessage(0);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {

                }
            });


        }catch (Exception e){
            e.printStackTrace();
        }

    }



    void setView(){

        try {
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(0).getString("logo"),poster1);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(1).getString("logo"),poster2);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(2).getString("logo"),poster3);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(3).getString("logo"),poster4);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(4).getString("logo"),poster5);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(5).getString("logo"),poster6);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(6).getString("logo"),poster7);
            ImageLoader.getInstance().displayImage(dataArray.getJSONObject(7).getString("logo"),poster8);


            text1.setTextSize(18);
            text2.setTextSize(18);
            text3.setTextSize(18);
            text4.setTextSize(18);
            text5.setTextSize(18);
            text6.setTextSize(18);
            text7.setTextSize(18);
            text8.setTextSize(18);

            text1.setText(dataArray.getJSONObject(0).getString("name"));
            text2.setText(dataArray.getJSONObject(1).getString("name"));
            text3.setText(dataArray.getJSONObject(2).getString("name"));
            text4.setText(dataArray.getJSONObject(3).getString("name"));
            text5.setText(dataArray.getJSONObject(4).getString("name"));
            text6.setText(dataArray.getJSONObject(5).getString("name"));
            text7.setText(dataArray.getJSONObject(6).getString("name"));
            text8.setText(dataArray.getJSONObject(7).getString("name"));
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

}
